// pages/goods_detail/index.js
import { request } from '../../request/http.js';
import regeneratorRuntime from '../../lib/runtime/runtime.js';

// 到本页码将点击时的id传过来，然后异步获取信息异步获取信息getGoodsInfo
// 异步获取信息getGoodsInfo   传入商品id 请求/goodsInfo    获得缓存里的collect   对比collect里id和我们点击进来的id判断是否收藏了    将是否收藏了赋值给goodsInfo
// 轮播图handlePreviewImage    使用了接口显示放大图片wx.previewImage
// 加入购物车handCartAdd    获取缓存cart购物车   遍历car如果存在就数量加一cart[i].goods_count += 1  如果不存在将获取到getGoodsInfo数据存入cart缓存里      然后显示加入成功
// 商品收藏获取collect缓存    获取到goodsInfo  通过商品id判断是否收藏过   如果收藏过再次点击就应该移除收藏   否则收藏进collect   弹出操作成功    设置isCollect:index==-1(即false)       collect存入缓存

//补充：对于收藏只有true  false    如果存在我们再次带你就就取消收藏， 如果不存在就收藏


Page({

  data: {
    // 商品详情信息
    goodsInfo: {},
    // 商品是否被收藏
    isCollect: true,
    //出现请求错误时
    statusCode:0
  },
  // 商品id
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { goods_id } = options;
    console.log(options);
    this.getGoodsInfo(goods_id);
  },
  // 获取商品详情
  async getGoodsInfo(goodsId) {
      const result = await request({ name: 'goodsInfo', data: { goods_id: goodsId } });
     console.log(result.statusCode);

      let collect = wx.getStorageSync('collect') || [];
      // 判断当前商品是否被收藏
      let { goods_id } = result.data.message;
      let isCollect = collect.some(v => v.goods_id == goods_id)
      console.log(result.data.message);
      this.setData({
        goodsInfo: result.data.message, isCollect, statusCode: result.statusCode
      })

    
      // wx.switchTab({//返回首页
      //   url: '../index/index',
      // }) 
    
    
  },
  // 轮播图预料
  handlePreviewImage(e) {
    let newArr = this.data.goodsInfo.pics.map(item => item.pics_mid)
    wx.previewImage({
      current: e.currentTarget.dataset.url,
      urls: newArr
    })
  },
  // 点击加入购物车
  handCartAdd() {
    // 获取缓存中购物车数组  [{goods_id:Number,goods_count:Number},...]
    let cart = wx.getStorageSync('cart') || [];
    let i = cart.findIndex(v => v.goods_id == this.data.goodsInfo.goods_id)
    if (i !== - 1) {
      // 存在
      // cart[i].goods_count += 1;
      wx.showToast({
        title: '已经在到购物车了~',
        icon: 'none',
        duration: 3000,
        mask: true,
      })
      return;
    } else {
      // 新商品
      let { goods_id, goods_name, goods_price,goods_number, goods_small_logo } = this.data.goodsInfo;
      let obj = { goods_id, goods_name, goods_price, goods_small_logo, checked: false, goods_count: goods_number }
      cart.push(obj)
    }
    wx.setStorageSync('cart', cart);
    wx.showToast({
      title: '加入成功',
      mask: true
    })
  },
  // 商品收藏
  handleAddCollect() {
    let collect = wx.getStorageSync('collect') || [];
    // 判断该商品是否被收藏过
    let { goods_id, goods_name, goods_price, goods_small_logo } = this.data.goodsInfo;
    let index = collect.findIndex(v => v.goods_id == goods_id);
    if (index != -1) {
      // 移除收藏
      collect.splice(index, 1)
    } else {
      collect.push({ goods_id, goods_name, goods_price, goods_small_logo })
    }
    wx.showToast({ title: "操作成功", icon: 'success', mask: true })
    this.setData({ isCollect: index == -1 })
    wx.setStorageSync('collect', collect);
  },
  handbesell(){
    wx.showToast({
      title: '已经被卖出了哦~',
      icon: 'none',
      duration: 3000,
      mask: true,
    })
  }
})